package world.criver.h50529.controller;

import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import world.criver.h50529.application.appAffirmPop;
import world.criver.h50529.data.daControllers;
import world.criver.h50529.data.daData;
import world.criver.h50529.data.daFxml;
import world.criver.h50529.function.funFiles;
import world.criver.h50529.module.moListen;

public class conPage2 {
    public VBox list;

    public void onAddListen() {
        daControllers.master.setCenter(daFxml.page2Add);
        daControllers.page2Add.ini();
    }
    public void updateList(){
        list.getChildren().clear();
        new funFiles().readListens();
        for(moListen mo: daData.listens){
            HBox hb=new HBox();
            hb.getStyleClass().add("HBox");
            Label l1=new Label(mo.getName()+"("+mo.getHotkey()+")");
            l1.setPrefWidth(216);
            hb.getChildren().add(l1);
            Label l2=new Label(mo.toString());
            l2.setPrefWidth(496-46);
            hb.getChildren().add(l2);
            Button forbidden=new Button(mo.isForbidden()?"启用":"禁用");
            forbidden.setOnAction(e->{
                mo.setForbidden(!mo.isForbidden());
                new funFiles().saveListen(mo);
                updateList();
            });
            hb.getChildren().add(forbidden);
            Button delete=new Button("删除");
            delete.setOnAction(e->{
                try {
                    new appAffirmPop("是否确认删除 " + mo.getName() + " 热键?", () -> {
                        Platform.runLater(() -> {
                            funFiles f = new funFiles();
                            f.deleteListen(mo.getName());
                            f.readEvents();
                            updateList();
                        });
                    }).start(new Stage());
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            hb.getChildren().add(delete);
            Button update=new Button("修改");
            update.setOnAction(e->{
                daControllers.master.setCenter(daFxml.page2Add);
                daControllers.page2Add.load(mo);
            });
            hb.getChildren().add(update);
            list.getChildren().add(hb);
        }
    }
}
